function loadPagination(){
	var max_pages = $(".max_pages").val();
	var show_by_page = parseInt($('.show_by_page').val());;
	var number_of_items = $('.list-item').size();
	var number_of_pages = Math.ceil(number_of_items/show_by_page);
	if(number_of_pages<=10){
		max_pages=10;	
	}
	var left_number = (max_pages/2)-1;
	var right_number = (number_of_pages-(max_pages/2));
	$( ".current_page" ).val(0);
	$( ".show_by_page" ).val(show_by_page);
	var navigation_html = '<a class="previous_link" href="javascript:previous();">'+
						  '<img src="images/prev.png" />Prev</a>';
	var current_link = 0;
	if(number_of_pages > max_pages){
		var half = (number_of_pages/2)-1;
		while(number_of_pages > current_link){
			if((current_link <= left_number)||(current_link >= right_number)){
				navigation_html += '<a class="page_link" href="javascript:go_to_page(' + 
									current_link +')" longdesc="' + current_link +'">'
									+ (current_link + 1) +'</a>';
			}else{
				navigation_html += '<a class="page_link" hidden="true" href="javascript:go_to_page(' + 
									current_link +')" longdesc="' + current_link +'">'
									+ (current_link + 1) +'</a>';
			}
			if(half<=current_link){
				navigation_html += '<span class="points" href="#" longdesc="-1">'
				+'...</span>';
				half=99999999;
			}
			current_link++;
		}
	}else{
		while(number_of_pages > current_link){
			navigation_html += '<a class="page_link" href="javascript:go_to_page(' + 
								current_link +')" longdesc="' + current_link +'">'
								+ (current_link + 1) +'</a>';
			current_link++;
		}
	}
	navigation_html += '<a class="next_link" href="javascript:next();">'+
					   'Next<img src="images/next.png" /></a>';
	$( ".list-pagination" ).html(navigation_html);
	$( ".list-pagination .page_link:first" ).addClass("active_page");
	$( ".list-item" ).css("display", "none");
	$( ".list-item").slice(0, show_by_page).css("display", "inline-block");
	$( ".previous_link" ).addClass("no_click");
	$( ".list-pagination a,.list-pagination span" ).addClass("ui-widget ui-widget-content");
	$( ".list-pagination a:first" ).addClass("ui-corner-left");
	$( ".list-pagination a:last" ).addClass("ui-corner-right");
}
function previous(){
	new_page = parseInt($('.current_page').val()) - 1;
	if($('.active_page').prev('.points').length==true){
		if($('.active_page').prev('.points').prev('.page_link').length==true){
			go_to_page(new_page);
		}
	}else{
		if($('.active_page').prev('.page_link').length==true){
			go_to_page(new_page);
		}
	}
	
}
function next(){
	new_page = parseInt($('.current_page').val()) + 1;
	if($('.active_page').next('.points').length==true){
		if($('.active_page').next('.points').next('.page_link').length==true){
			go_to_page(new_page);
		}
	}else{
		if($('.active_page').next('.page_link').length==true){
			go_to_page(new_page);
		}
	}
}
function go_to_page(page_num){
	$( ".next_link, .previous_link" ).removeClass("no_click");
	var max_pages = $(".max_pages").val();
	var show_by_page = parseInt($('.show_by_page').val());
	var number_of_items = $('.list-item').size();
	var number_of_pages = Math.ceil(number_of_items/show_by_page);
	if(number_of_pages<=10){
		max_pages=10;	
	}
	var half= number_of_pages/2;
	var left_number = (max_pages/2)-1;
	var right_number = (number_of_pages-(max_pages/2));
	start_from = page_num * show_by_page;
	end_on = start_from + show_by_page;
	if(number_of_pages>max_pages){
		if(page_num>left_number && page_num<right_number){
			if(page_num<=half-1){
				$('.page_link').slice((page_num-(max_pages/2)+1),page_num).css("display","inline");
				$('.page_link').slice(0,(page_num-(max_pages/2)+1)).css("display","none");
				$('.page_link').slice(half,number_of_pages).css("display","inline");
				$('.page_link').slice(page_num+1,right_number).css("display","none");
			}else if(page_num>=half){
				$('.page_link').slice((page_num+1),(page_num+(max_pages/2))).css("display","inline");
				$('.page_link').slice((page_num+(max_pages/2)),number_of_pages).css("display","none");
				$('.page_link').slice(0,left_number+1).css("display","inline");
				$('.page_link').slice(left_number+1,page_num).css("display","none");
			}
			$('.page_link[longdesc=' + page_num +']').css("display","inline");
		}else{
			$('.page_link').slice(0,left_number-1).css("display","inline");
			$('.page_link').slice(half+1,number_of_pages).css("display","inline");
			$('.page_link').slice((left_number+1),right_number).css("display","none");
		}
	}
	$('.list-item').css('display', 'none').slice(start_from, end_on).css('display', 'inline-block');
	$('.page_link[longdesc=' + page_num +']').addClass('active_page').siblings('.active_page').removeClass('active_page');
	$('.current_page').val(page_num);
	$( ".item-content" ).hide().show("slide",450);
	$( ".item-image" ).hide().show("fade",600);
	$( ".item-mark" ).hide().show("bounce",1000);
	$( ".map-container" ).hide().show("fade",700);
	if($( ".active_page" ).next( ".page_link" ).length==false){
		if($( ".active_page" ).next( ".points" ).html()==="..."){
		}else{
			$( ".next_link" ).addClass("no_click");
		}
	}
	if($( ".active_page" ).prev( ".page_link" ).length==false){
		if($( ".active_page" ).prev( ".points" ).html()==="..."){
		}else{
			$( ".previous_link" ).addClass("no_click");
		}
	}
	if(show_by_page==3){
		var a=start_from;
		var b=start_from+1;
		var c=end_on-1;
		var idx = [a,b,c];
		loadMap(idx);
	}else if(show_by_page==4){
		var a=start_from;
		var b=start_from+1;
		var c=start_from+2;
		var d=end_on-1;
		var idx = [a,b,c,d];
		loadMap(idx);
	}
}
